package com.example.administrator.videotest2.tvshow;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.text.TextUtils;
import android.view.View;
import android.widget.AdapterView;
import android.widget.GridView;

import com.example.administrator.videotest2.PlayerActivity;
import com.example.administrator.videotest2.R;
import com.example.administrator.videotest2.entity.TextEntity;
import com.example.administrator.videotest2.util.JsonUtil;
import com.example.administrator.videotest2.util.To_playerAdapter;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.umeng.analytics.MobclickAgent;

import java.util.ArrayList;
import java.util.List;

public class TvActivity extends AppCompatActivity {


    private List<TextEntity> datalist=new ArrayList<>();
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_tv);
        this.runOnUiThread(new Runnable() {
            @Override
            public void run() {
                init();
                To_playerAdapter to_playerAdapter = new To_playerAdapter(TvActivity.this, R.layout.toplayer_item, datalist);
                GridView listView = (GridView)findViewById(R.id.tv_list);
                listView.setAdapter(to_playerAdapter);
                listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                    @Override
                    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                        TextEntity item = (TextEntity) parent.getItemAtPosition(position);
                        Intent intent = new Intent(TvActivity.this,PlayerActivity.class);
                        intent.putExtra("url", item.getPlurl().toString());
                        intent.putExtra("title",item.getNum());
                        startActivity(intent);
                    }
                });

            }
        });

    }

    public void init(){

        String JsonContext =new JsonUtil().getJson("tv_json.json",TvActivity.this);
        if (!TextUtils.isEmpty(JsonContext)){
            try {
                Gson gson=new Gson();

               datalist= gson.fromJson(JsonContext, new TypeToken<List<TextEntity>>() {}.getType());
            } catch (Exception e) {

            }
        }
    }


    @Override
    protected void onDestroy() {
        super.onDestroy();
    }


    public void onResume() {
        super.onResume();
        MobclickAgent.onPageStart("SplashScreen");
        MobclickAgent.onResume(this);
    }
    @Override
    public void onPause() {
        super.onPause();
        MobclickAgent.onPageEnd("SplashScreen");
        MobclickAgent.onPause(this);
    }

}
